package com.fhz.day002;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView hello;
    private TextView hello1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hello1 = (TextView) findViewById(R.id.hello1);
        hello1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createPopupMenu();
            }
        });
        hello = (TextView) findViewById(R.id.hello);
        //注册
        registerForContextMenu(hello);

    }

    public void createPopupMenu(){
        PopupMenu popupMenu = new PopupMenu(this, hello1);

        popupMenu.inflate(R.menu.menu);

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //点击事件
                switch (item.getItemId()){
                    //定义的id
                    case R.id.one:
                        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.two:
                        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                }

                return false;

            }
        });

        popupMenu.show();
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu,menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        //点击事件
        switch (item.getItemId()){
            //定义的id
            case R.id.one:
                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.two:
                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;

        }

        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //和系统绑定
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        //点击事件
        switch (item.getItemId()){
            //定义的id
            case R.id.one:
                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.two:
                Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                break;

        }

        return super.onOptionsItemSelected(item);

    }

    public void btn(View view) {
        PopupWindow popupWindow = new PopupWindow(this);
        View inflate = LayoutInflater.from(this).inflate(R.layout.popup_windon_layout, null);
        //三要素
        popupWindow.setContentView(inflate);
        popupWindow.setHeight(300);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);


//        popupWindow.showAsDropDown(hello1);


        //设置动画
        popupWindow.setAnimationStyle(R.style.pop_window);

        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);//编辑框聚焦

        View mainView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);

        popupWindow.showAtLocation(mainView, Gravity.CENTER,0,0);

    }

}